<?php

namespace App\Models\Reservation;

use App\Models\Hospital;
use App\Models\Patient;
use App\Project\Traits\Model\WhereTimeBetween;
use App\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Reservation extends Model
{
    use SoftDeletes, WhereTimeBetween;

    protected $fillable = [
        'doctor_id', 'hospital_id', 'patient_id', 'time', 'am', 'start', 'end',
        'room', 'purpose', 'remark', 'status'
    ];

    protected $table = 'st_reservations';

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function doctor()
    {
        return $this->belongsTo(User::class, 'doctor_id')
                    ->select(['id', 'name', 'account', 'hospital_id',]);
    }

    public function schedule()
    {
        return $this->belongsTo(Schedule::class, 'doctor_id', 'doctor_id');
    }

    public function scopeWhereDoctor(Builder $builder, array $where)
    {
        return $builder->whereHas('doctor', function (Builder $builder) use ($where) {
                $builder->where($where);
            }
        );
    }

    public function hospital()
    {
        return $this->belongsTo(Hospital::class)
                    ->select(['id', 'name', 'type', 'clinic']);
    }

    /**
     * 按当前登录用户的权限设置医院的搜索条件
     * @param \Illuminate\Database\Eloquent\Builder $builder
     * @param                                       $hospital
     * @return $this|\Illuminate\Database\Eloquent\Builder
     */
    public function scopeWhereHospital(Builder $builder, $hospital = null)
    {
        if (\Auth::user()->isAn('admin')) {

            return $hospital ? $builder->where(['hospital_id' => $hospital]) : $builder;
        }

        return $builder->where('hospital_id', \Auth::user()->hospital_id);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function patient()
    {
        return $this->belongsTo(Patient::class, 'patient_id')
                    ->select(
                        [
                            'id', 'name', 'sex', 'age', 'birthday', 'phone', 'hospital_id',
                            'created_at', 'updated_at',
                        ]
                    );
    }

    public function scopeWherePatient(Builder $builder, array $where)
    {
        if (\Auth::user()->isNotAn('admin')) {
            $where[] = ['st_reservations.hospital_id', '=', \Auth::user()->hospital_id];
        }

        return $builder->whereHas(
            'patient',
            function (Builder $builder) use ($where) {
                $builder->where($where);
            }
        );
    }

    public function getStatusAttribute($key)
    {
        $value = [1 => '未发送', 2 => '已发送', 3 => '未确认', 4 => '已确认'][$key];

        return ['key' => $key, 'value' => $value];
    }

    /**
     * @param $patient_id
     * @param $time
     * @return bool
     */
    public function hasBeenCreated($patient_id, $time)
    {
        return $this->where(['patient_id' => $patient_id, 'time' => $time])->count() ? true : false;
    }
}
